package com.qihetimes.earthangel.common;

import lombok.Data;
import org.springframework.http.HttpStatus;

/**
 * 通用返回对象
 *
 * @author ：f4cklangzi@gmail.com
 * @date ：Created At 2021/12/4
 */
@Data
public class ResponseResult<T> {
    private int code;
    private String message;
    private T data;
    
    protected ResponseResult() {
    }
    
    protected ResponseResult(HttpStatus httpStatus, String message, T data) {
        this.code = httpStatus.value();
        this.message = message;
        this.data = data;
    }
    
    
    /**
     * 成功无返回结果
     */
    public static <T> ResponseResult<T> success() {
        return new ResponseResult<T>(HttpStatus.OK, HttpStatus.OK.getReasonPhrase(), null);
    }
    
    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> ResponseResult<T> success(T data) {
        return new ResponseResult<T>(HttpStatus.OK, HttpStatus.OK.getReasonPhrase(), data);
    }
    
    /**
     * 成功返回结果
     *
     * @param data    获取的数据
     * @param message 提示信息
     */
    public static <T> ResponseResult<T> success(T data, String message) {
        return new ResponseResult<T>(HttpStatus.OK, message, data);
    }
    
    /**
     * 失败返回结果
     *
     * @param httpStatus 错误码
     * @param message    错误信息
     */
    public static <T> ResponseResult<T> failed(HttpStatus httpStatus, String message) {
        return new ResponseResult<T>(httpStatus, message, null);
    }
    
    /**
     * 失败返回结果
     *
     * @param message 提示信息
     */
    public static <T> ResponseResult<T> failed(String message) {
        return new ResponseResult<T>(HttpStatus.BAD_REQUEST, message, null);
    }
    
    /**
     * 失败返回结果
     */
    public static <T> ResponseResult<T> failed() {
        return failed(HttpStatus.BAD_REQUEST, "异常");
    }
}
